﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Model;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class KQGLForm : Form
    {
        private OrgInfo _org;

        public KQGLForm()
        {
            InitializeComponent();
            InitType();
        }

        public KQGLForm(OrgInfo orginfo)
            : this()
        {
            if (orginfo != null && !string.IsNullOrEmpty(orginfo.ID))
            {
                _org = orginfo;
                this.txtOrgCode.Text = _org.OrgCode;
                this.txtOrgName.Text = _org.OrgName;
                this.cbType.SelectedValue = orginfo.OrgType;
                this.txtOrgCode.ReadOnly = true;
            }
        }

        /// <summary>
        /// 初始化类型下拉列表
        /// </summary>
        private void InitType()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));

            dt.Rows.Add((int)OrgType.KQ, "库区");
            dt.Rows.Add((int)OrgType.TCC, "停车场");

            cbType.DataSource = dt;
            cbType.ValueMember = "id";
            cbType.DisplayMember = "name";
            cbType.DropDownStyle = ComboBoxStyle.DropDownList;
            cbType.SelectedValue = (int)OrgType.KQ;
        }

        private bool CheckInput()
        {
            if (string.IsNullOrWhiteSpace(txtOrgCode.Text))
            {
                MessageBox.Show("库区编码不能为空！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(txtOrgName.Text))
            {
                MessageBox.Show("库区名称不能为空！");
                return false;
            }
            if (cbType.SelectedValue == null)
            {
                MessageBox.Show("类型不能为空！");
                return false;
            }
            if (_org == null)//新增
            {
                string strSQL = "select ID from T_SYS_ORG where ORGCODE = '" + txtOrgCode.Text + "'";
                DataTable dtKQ = DataAccess.ExecuteDataTable(strSQL);
                if (dtKQ != null && dtKQ.Rows.Count > 0)
                {
                    MessageBox.Show("该库区编码已存在！");
                    return false;
                }
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                string strOrgName = txtOrgName.Text;
                string strOrgCode = txtOrgCode.Text;
                int iType = (int)cbType.SelectedValue;
                if (_org != null && !string.IsNullOrEmpty(_org.ID))//修改
                {
                    DataAccess.ExecuteNonQuery("update T_SYS_ORG set ORGNAME='" + strOrgName + "',TYPE='" + iType + "' where ID = '" + _org.ID + "'");
                }
                else//新增
                {
                    DataAccess.ExecuteNonQuery("insert into T_SYS_ORG(ID,ORGCODE,ORGNAME,TYPE) values('" + Guid.NewGuid().ToString() + "','" + strOrgCode + "','" + strOrgName + "','" + iType + "')");
                }
                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
